<?php 
/**
 * Model is the base class for data models.
 *
 * @copyright Copyright (c) 2015 Swolf Software.
 * @author Liu Xiao<seanliu707@gmail.com>
 */

class Model extends MysqlModel
{
    private static $filterObj = false;
    
	public function __construct()
	{
		parent::__construct();
	}
	
	/**
	 * Check and load the Filter class if it is not loaded
	 *
	 * @see	swolf/filter/Filter
	 */
	protected static function loadFilter()
	{
	    if (self::$filterObj == false) {
	        Swolf::load('filter.Filter');
	        self::$filterObj = true;
	    }
	}
	
	/**
	 * @see swolf/widgets/IpInfo
	 * @return Ambigous <object, mixed, multitype:>
	 */
	protected function ip()
	{
	    return Swolf::createObject('widgets.IpInfo');
	}
	
	/**
	 * 
	 * @param array $sender the mail sender,
	 *              for example array('no-replay', //username
	 *                                'password',  //password
	 *                                false        //can relpy
	 *                                )
	 * @param array $recipient the mail recipient, 
	 *              for example array('joe@example.com', 'Joe User')
	 * @param string $title the mail object
	 * @param string $content
	 */
	protected function sendMail($sender, $recipient, $title, $content)
	{
	    require dirname(dirname(__DIR__)) . DIRECTORY_SEPARATOR . 'PHPMailer' . DIRECTORY_SEPARATOR . 'PHPMailerAutoload.php';
	    
	    $mail = new PHPMailer;
	    
	    //$mail->SMTPDebug = 3;                          // Enable verbose debug output
	    
	    $mail->isSMTP();                                 // Set mailer to use SMTP
	    $mail->Host = 'smtp.mxhichina.com';              // Specify main and backup SMTP servers
	    $mail->SMTPAuth = true;                               // Enable SMTP authentication
	    $mail->Username = $sender[0] . '@hpcer.com';     // SMTP username
	    $mail->Password = $sender[1];                    // SMTP password
	    $mail->SMTPSecure = 'tls';                       // Enable TLS encryption, `ssl` also accepted
	    $mail->Port = 25;                                // TCP port to connect to
	    
	    $mail->From = $sender[0] . '@hpcer.com';
	    $mail->FromName = 'Hpcer';
	    $mail->addAddress($recipient[0], $recipient[1]);     // Add a recipient
	    
	    if ($sender[2] == true) {
	        $mail->addReplyTo($sender[0] . '@hpcer.com', $sender[0]);
	    }
	    
	    /*
	    $mail->addAttachment('/var/tmp/file.tar.gz');         // Add attachments
	    $mail->addAttachment('/tmp/image.jpg', 'new.jpg');    // Optional name
	    */
	    $mail->isHTML(true);                                  // Set email format to HTML
	    
	    $mail->Subject = $title;
	    $mail->Body    = $content;
	    $mail->AltBody = $content;
	    
	    if(!$mail->send()) {
	        return FALSE;
	        //echo 'Mailer Error: ' . $mail->ErrorInfo;
	    } 
	    
	    return TRUE;
	}
}